package com.chai.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.chai.dao.OrderSettingDao;
import com.chai.pojo.OrderSetting;
import com.chai.service.OrderSettingService;
import com.chai.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void addBatch(List<OrderSetting> datalist) {
        if (datalist != null && datalist.size() != 0) {
            for (OrderSetting orderSetting : datalist) {
                OrderSetting orderSettingdb = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
                if (orderSettingdb == null) {
                    orderSettingDao.add(orderSetting);
                } else {
                    orderSettingDao.update(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map<String, Integer>> getOrderSettingByMonth(String date) {

        String start = date + "-01";
        String end = DateUtils.getLastDayOfMonth(date);

        Map dateParam = new HashMap();
        dateParam.put("start", start);
        dateParam.put("end", end);

        List<Map<String, Integer>> resultList = new ArrayList<>();
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(dateParam);
        for (OrderSetting orderSetting : list) {
            Map<String, Integer> mapobj = new HashMap<>();
            mapobj.put("date", orderSetting.getOrderDate().getDate());
            mapobj.put("number", orderSetting.getNumber());
            mapobj.put("reservations", orderSetting.getReservations());
            resultList.add(mapobj);
        }
        return resultList;
    }

    @Override
    public void saveOrUpdate(OrderSetting orderSetting) {
        OrderSetting orderSettingdb = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
        if (orderSettingdb==null){
            orderSettingDao.add(orderSetting);
        }else {
            orderSettingDao.update(orderSetting);
        }
    }
}
